package club.mzywucai.blog.file_system.controller;

import club.mzywucai.blog.file_system.pojo.domain.JsonData;
import club.mzywucai.blog.file_system.pojo.vo.UserVo;
import club.mzywucai.blog.file_system.service.LoginAndRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author mzywucai
 * @Description 登录注册的Controller
 * @date 2019/2/9
 */
@Controller
public class LoginAndRegisterController {
    @Autowired
    LoginAndRegisterService loginAndRegisterService;

    /**
     * @Description 帮助前台确认是否登录
     * @author mzywucai
     * @time 2019-02-10
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/checkLogin")
    @ResponseBody
    public Object checkLogin(HttpSession session) {

        if (null != session.getAttribute("user")) {
            return JsonData.buildSuccess(0, "ok", "已登录");
        }

        return JsonData.buildError("未登录");
    }
    
    
    /**
     * @Description 帮助登录用户获取session中的userVo内容
     * @author mzywucai
     * @time 2019-02-10
     * @see 
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/getSession")
    @ResponseBody
    public Object getSession(HttpSession session) {
        if (null != session.getAttribute("user")) {
            UserVo user = (UserVo) session.getAttribute("user");

            return JsonData.buildSuccess(0, user, "获取成功");
        }

        return JsonData.buildError("获取失败");
    }

    /**
     * @Description 修改静默的状态
     * @author mzywucai
     * @time 2019-02-15
     * @see 
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/updateSilentStatus/{silentStatus}")
    @ResponseBody
    public Object updateSilentStatus(HttpSession session, @PathVariable("silentStatus") int silentStatus) {
        JsonData json = loginAndRegisterService.updateUserPersonalSilentStatus(session, silentStatus);

        return json;
    }

    /**
     * @Description 获取用户的头像地址：
     *      考虑了很久，这个必须要单独获取，如果放到session的话，就失去了时效性。
     *      即使前台在上传的时候做了修改，页面往复点击一下，头像还是会变回去，只有下一次登陆的时候才会变回最新的！
     *      不科学！所以单独获取头像地址。
     *
     *      但是又考虑到请求的损耗，故在userPo中依然留着头像的地址，只是在展示的位置请求最新的这个photoURI接口就行了。
     *      请求这个接口之后还能对session中的photoURI做一个更新。
     * @author mzywucai
     * @time 2019-02-10
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/user/getPhotoURI")
    @ResponseBody
    public Object getPhotoURI(HttpSession session) {
        if (null != session.getAttribute("user")) {
            JsonData json = loginAndRegisterService.getPhotoURI(session);

            return json;
        }

        return JsonData.buildError("获取失败");
    }
    
    /**
     * @Description 帮助前台注销用户，移除session
     * @author mzywucai
     * @time 2019-02-10
     * @see 
     * @return   java.lang.Object
     * @params  [session]
     */
    @GetMapping("/api/user/removeSession")
    @ResponseBody
    public Object removeSession(HttpSession session) {
        if (null != session.getAttribute("user")) {
            session.removeAttribute("user");

            return JsonData.buildSuccess(0, "ok", "注销成功");
        }

        return JsonData.buildError("注销失败");
    }


    /**
     * @Description 登录
     * @author mzywucai
     * @time 2019-02-09
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/login")
    @ResponseBody
    public Object login(String email, String password, HttpSession session) {
        JsonData json = loginAndRegisterService.login(email, password, session);

        return json;
    }

    /**
     * @Description 注册
     * @author mzywucai
     * @time 2019-02-09
     * @see 
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/register")
    @ResponseBody
    public Object register(String email, String password, HttpServletRequest request) {
        JsonData json = loginAndRegisterService.register(email, password, request);

        return json;
    }

    /**
     * @Description 通过UUID找到要激活的用户
     * @author mzywucai
     * @time 2019-02-09
     * @see 
     * @return   java.lang.Object
     * @params  [UUID]
     */
    @GetMapping("/api/activationCheck/{uuid}")
    public String activationBefore(@PathVariable("uuid") String UUID, ModelMap map) {
        String email = loginAndRegisterService.activationBefore(UUID);

        if (null == email || email.trim().equals("")) {
            return "error.html";
        }

        map.addAttribute("email", email);
        map.addAttribute("uuid", UUID);

        return "activate-account.html";
    }


    /**
     * @Description 通过UUID找到要激活的用户
     * @author mzywucai
     * @time 2019-02-09
     * @see
     * @return   java.lang.Object
     * @params  [UUID]
     */
    @PostMapping("/api/activation")
    @ResponseBody
    public Object activation(@RequestParam("uuid") String UUID, @RequestParam("username") String username) {
        JsonData json = loginAndRegisterService.activation(UUID, username);

        return json;
    }
}
